import numpy as np
import matplotlib.pyplot as plt
from sklearn.svm import SVC

# load
data = np.loadtxt('ex2data1.txt', delimiter=',')
m = len(data)
x = data[:, :-1]
y = data[:, -1]

# scale
mu = x.mean(axis=0)
sigma = x.std(axis=0)
x -= mu
x /= sigma

# shuffle
np.random.seed(1)
a = np.random.permutation(m)
x = x[a]
y = y[a]

# split
m_train = int(0.7 * m)
x_train, x_test = np.split(x, [m_train])
y_train, y_test = np.split(y, [m_train])

clf = SVC(C=1, kernel='rbf', gamma=2)
clf.fit(x_train, y_train)
print(f'Training score = {clf.score(x_train, y_train)}')
print(f'Testing score = {clf.score(x_test, y_test)}')

print(np.mean(y_train == (clf.predict(x_train) > 0.5)))
print(np.mean(y_test == (clf.predict(x_test) > 0.5)))

print(f'Support vector idx: {clf.support_}')
print(f'Support vector number: {clf.n_support_}')

x1_min, x1_max = x[:, 0].min(), x[:, 0].max()
x2_min, x2_max = x[:, 1].min(), x[:, 1].max()
xx, yy = np.mgrid[x1_min:x1_max:200j, x2_min:x2_max:200j]
xxyy = np.c_[xx.ravel(), yy.ravel()]
zz = clf.decision_function(xxyy).reshape(xx.shape)
plt.scatter(x[:, 0], x[:, 1], c=y, edgecolors='k')
plt.contour(xx, yy, zz > 0)
plt.show()
